package com.wudl.govern.controller.monitor;

import cn.hutool.system.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wudl.common.api.CommonResult;
import com.wudl.govern.config.DataDictUtils;
import com.wudl.govern.domain.ControlTask;
import com.wudl.govern.service.ControlTaskService;
import com.wudl.govern.service.ErrorTaskLogService;
import com.wudl.govern.utils.StringHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ：wudl
 * @date ：Created in 2022-04-05 11:43
 * @description：质量监控任务controller
 * @modified By：
 * @version: 1.0
 */
@Api
@RestController
@RequestMapping("/controlTask")
@Slf4j
public class ControlTaskController {

    @Autowired
    private ControlTaskService controlTaskService;
    @Autowired
    private ErrorTaskLogService errorTaskLogService;
    @Autowired
    private DataDictUtils dataDictUtils;



    /**
     * 按条件分页查询
     *
     * @param controlTask
     * @return
     */
    @PostMapping("/selectPage")
    @ApiOperation(value = "按条件分页查询", notes = "按条件分页查询")
    public CommonResult<List<Map<String, Object>>> selectPage(@ApiParam(value = "质量规则任务对象查询条件") @RequestBody ControlTask controlTask) {
        List<Map<String, Object>> maps = controlTaskService.selectPage(controlTask);
        return CommonResult.success(maps);
    }

    /**
     * 修改一条记录
     *
     * @param record
     * @return
     */
    @PostMapping("/updateOne")
    @ApiOperation(value = "修改一条质量任务", notes = "修改一条质量任务")
    public CommonResult updateOne(@RequestBody @ApiParam(value = "质量任务对象") @Validated ControlTask record, BindingResult bindingResult, HttpServletRequest request) {
        UserInfo userInfo = new UserInfo();
        Object o = controlTaskService.updateOne(record, bindingResult, userInfo);
        return CommonResult.success(o);
    }

    /**
     * 删除一条质量监控任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/deleteOne")
    @ApiOperation(value = "删除一条质量监控任务", notes = "删除一条质量监控任务")
    public CommonResult<String> deleteOne(@ApiParam(value = "任务id") @RequestParam(value = "taskId") Integer taskId) {
        String s = controlTaskService.deleteOne(taskId);
        return CommonResult.success(s);
    }

    /**
     * 新增一条记录
     *
     * @param record
     * @return
     */
    @PostMapping("/insertOne")
    @ApiOperation(value = "新增一条质量监控任务", notes = "新增一条质量监控任务")
    public CommonResult<Object> insertOne(@RequestBody @ApiParam(value = "质量监控任务对象") @Validated ControlTask record, BindingResult bindingResult, HttpServletRequest request) {
        UserInfo userInfo = new UserInfo();
        Object o = controlTaskService.insertOne(record, bindingResult, userInfo);
        return CommonResult.success(o);
    }

    /**
     * 关闭一条任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/pauseTask")
    @ApiOperation(value = "关闭一条任务", notes = "关闭一条任务")
    public CommonResult<Object> pauseTask(@ApiParam(value = "任务id") @RequestParam("taskId") Integer taskId) {
        String s = controlTaskService.pauseTask(taskId);
        return  CommonResult.success(s);
    }

    /**
     * 查询一条任务监控
     *
     * @param taskId
     * @return
     */
    @GetMapping("/selectOne")
    @ApiOperation(value = "查询一条任务监控", notes = "查询一条任务监控")
    public CommonResult<ControlTask> selectOne(@ApiParam(value = "任务id") @RequestParam("taskId") Integer taskId) {
        ControlTask controlTask = controlTaskService.selectOne(taskId);
        return  CommonResult.success(controlTask);
    }

    /**
     * 开启监控任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/startTask")
    @ApiOperation(value = "开启监控任务", notes = "开启监控任务")
    public CommonResult<Object> startTask(@ApiParam(value = "任务id") @RequestParam("taskId") Integer taskId) {

        Object o = controlTaskService.startTask(taskId);
        return CommonResult.success(o);
    }

    /**
     * 查询执行记录
     *
     * @param params
     * @return
     */
    @PostMapping("/selectTaskRecord")
    @ApiOperation(value = "查询执行记录", notes = "查询执行记录")
    public CommonResult<Map> selectTaskRecord(@RequestBody @ApiParam(value = "查询条件") Map<String, Object> params) {
        Map map = controlTaskService.selectTaskRecord(params);
        return CommonResult.success(map);
    }

    /***
     * 查询执行任务信息
     * @param param              jobId
     * @return
     */
    @RequestMapping(value = "/selectTask")
    public CommonResult<Map> selectTaskInfo(@RequestBody JSONObject param){
        String jobId = param.getString("jobId");
        if(StringHelper.isEmpty(jobId)){
            return CommonResult.failed();
        }
        Map<String, Object> stringObjectMap = controlTaskService.selectTaskInfo(jobId);
        return CommonResult.success(stringObjectMap);
    }

    /***
     * 批量添加错误规则数据
     * @param "rules"
     * @return
     */
    @RequestMapping(value = "/batchInsertErrorLog")
    public CommonResult batchInsertErrorLog(@RequestBody JSONObject param){
        String rules = JSON.toJSONString(param.get("rules"));
        if(StringHelper.isEmpty(rules)){
            return CommonResult.failed();
        }
        Integer integer = errorTaskLogService.batchInsertErrorLog(rules);
        return CommonResult.success();
    }

    /***
     * 插入监控报告信息（流程正常完成）
     * @param "report"                       报告信息
     * @param "reportDetails"                 报告详情列表
     * @return
     */
    @RequestMapping(value = "/insertReports")
    public CommonResult<Object> insertReports(@RequestBody JSONObject param){
        String report = param.getString("report");
        String reportDetails = param.getString("reportDetails");
        String finalScore = param.getString("finalScore");
        if(StringHelper.isEmpty(report+"")){
            return CommonResult.failed();
        }
        if(StringHelper.isEmpty(reportDetails+"")){
            return CommonResult.failed();
        }
        Integer integer = errorTaskLogService.insertReports(report, reportDetails, finalScore);
        return CommonResult.success();
    }

    /***
     * 添加质量异常单
     * @param "exception"
     * @return
     */
    @RequestMapping(value = "/insertExceptionReport")
    public Object insertExceptionReport(@RequestBody JSONObject param){
        if(param==null || param.size()<1){
            return CommonResult.failed();
        }
        Integer integer = errorTaskLogService.insertExceptionReport(param);
        return CommonResult.success();
    }

    /***
     * 记录错误数据文件地址
     * @param "errorFile"
     * @return
     */
    @RequestMapping(value = "/insertErrorDataFiles")
    public Object insertErrorDataFiles(@RequestBody JSONObject param){
        String errorFile = param.getString("errorFile");
        if(StringHelper.isEmpty(errorFile)){
            return CommonResult.failed();
        }
        Integer integer = errorTaskLogService.insertErrorDataFiles(errorFile);
        return CommonResult.success();
    }

    /***
     * 插入监控报告信息（流程未正常完成）
     * @param param
     * @return
     */
    @RequestMapping(value = "/insertReportByError")
    public Object insertReportByError(@RequestBody JSONObject param){
        String report = param.getString("report");
        String reportDetail = param.getString("reportDetail");
        if(StringHelper.isEmpty(report+"")){
            return CommonResult.failed();
        }
        if(StringHelper.isEmpty(reportDetail+"")){
            return CommonResult.failed();
        }
        Integer integer = errorTaskLogService.insertReportByError(report, reportDetail);
        return CommonResult.success();
    }

    /***
     * 查询质量概览
     * @param param
     * @return
     */
    @RequestMapping(value = "/qualityOverview")
    public Object selectQualityOverview(@RequestBody JSONObject param){
        Object obj = controlTaskService.selectQualityOverview(param);
        return obj;
    }
}
